#include "stdafx.h"
#include "ObjectTexture.h"
#include "ImageTGA.h"
#include "Constant.h"

cObjectTexture::cObjectTexture()
{
	m_uiaTextureID = NULL;
	m_uiNumTexture = 0;
}

void cObjectTexture::InitTexture2D(char **fileSrc, unsigned int count)
{
	ImageTGA *ImgSrcData = new ImageTGA[count];
	m_uiaTextureID = new GLuint[count];
	glGenTextures(count, m_uiaTextureID);

	for(int i=0; i<count; i++)
	{
		ImgSrcData[i].szTexSrc = LoadTGA(fileSrc[i], &ImgSrcData[i].iWidth, &ImgSrcData[i].iHeight, &ImgSrcData[i].iBPP);
		glBindTexture(GL_TEXTURE_2D, m_uiaTextureID[i]);
		if(ImgSrcData[i].iBPP == 24)
		{
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, ImgSrcData[i].iWidth, ImgSrcData[i].iHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, ImgSrcData[i].szTexSrc);
		}
		else if(ImgSrcData[i].iBPP == 32)
		{
			glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, ImgSrcData[i].iWidth, ImgSrcData[i].iHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, ImgSrcData[i].szTexSrc);
		}
		else
		{
			return ;
		}
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glBindTexture(GL_TEXTURE_2D, 0);
	}

	SAFE_DEL_ARRAY(ImgSrcData);
}

void cObjectTexture::InitTextureCube(char **fileSrc)
{
	m_uiaTextureID = new GLuint[1];

	ImageTGA *ImgSrcData = new ImageTGA[6];

	glGenTextures(1, m_uiaTextureID);
	glBindTexture(GL_TEXTURE_CUBE_MAP, m_uiaTextureID[0]);
	for(int i=0; i<6; i++)
	{
		ImgSrcData[i].szTexSrc = LoadTGA(fileSrc[i], &ImgSrcData[i].iWidth, &ImgSrcData[i].iHeight, &ImgSrcData[i].iBPP);
		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGB, ImgSrcData[i].iWidth, ImgSrcData[i].iHeight, 0, GL_RGB, GL_UNSIGNED_BYTE, ImgSrcData[i].szTexSrc);
	}

	glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glBindTexture(GL_TEXTURE_CUBE_MAP, 0);

	SAFE_DEL_ARRAY(ImgSrcData);
}

GLuint *cObjectTexture::GetTextureID()
{
	return m_uiaTextureID;
}

cObjectTexture::~cObjectTexture()
{
	SAFE_DEL_ARRAY(m_uiaTextureID);
}